package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuImage;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.service.SkuInfoService;
import com.atguigu.gmall.product.service.SpuImageService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("SKU")
@RequestMapping("/admin/product")
@RestController
public class BaseSkuController {

    @Autowired
    SpuImageService spuImageService;

    @Autowired
    SpuSaleAttrService spuSaleAttrService;

    @Autowired
    SkuInfoService skuInfoService;


    /**
     * 根据spuId获取图片列表
     * @param spuId
     * @return
     */
    @GetMapping("/spuImageList/{spuId}")
    public Result getSpuImageList(@PathVariable("spuId") Long spuId){
        List<SpuImage> list = spuImageService.list(new QueryWrapper<SpuImage>().eq("spu_id", spuId));
        return Result.ok(list);
    }

    /**
     * 根据spuId获取销售属性
     */
    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result getspuSaleAttrList(@PathVariable("spuId") Long spuId){
        List<SpuSaleAttr> spuSaleAttrs  = spuSaleAttrService.getspuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrs);
    }

    /**
     * 添加sku
     * @return
     */
    @PostMapping("/saveSkuInfo")
    public Result postsaveSkuInfo(@RequestBody SkuInfo skuInfo){
        skuInfoService.postsaveSkuInfo(skuInfo);
        return Result.ok();
    }

    /**
     * 获取sku分页列表
     */
    @GetMapping("/list/{page}/{limit}")
    public Result getlist(@PathVariable("page")Long page,
                          @PathVariable("limit")Long limit){
        Page<SkuInfo> page1 = skuInfoService.page(new Page<SkuInfo>(page, limit));
        return Result.ok(page1);
    }

    /**
     * 上架
     */
    @GetMapping("/onSale/{skuId}")
    public Result getonSale(@PathVariable("skuId")Long skuId){
        SkuInfo byId = skuInfoService.getById(skuId);
        byId.setIsSale(1);
        skuInfoService.updateById(byId);
        return Result.ok();
    }


    /**
     * 下架
     */
    @GetMapping("/cancelSale/{skuId}")
    public Result getcancelSale(@PathVariable("skuId")Long skuId){
        SkuInfo byId = skuInfoService.getById(skuId);
        byId.setIsSale(0);
        skuInfoService.updateById(byId);
       return Result.ok();
    }

}
